﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Tycho.Runtime;
using System.IO;
using NUnit.Framework;

namespace Tycho.UnitTests.Evaluation {
    public class Script {
        public static void Test (string scriptPath) {
            Test (File.ReadAllText (scriptPath + ".results"), File.ReadAllText (scriptPath + ".results"));
        }

        public static void Test (string script, string expectedResult) {
            script = script.Trim ();
            expectedResult = expectedResult.Trim ();

            TopLevel topLevel = new TopLevel ();
            StringWriter output = new StringWriter ();
            using (new ThreadContext ()) {
                ThreadContext.Current.SetProperty (Symbols.RuntimePrintStream, new NativeObject<TextWriter> (output));

                AnyObject result = topLevel.Evaluate (script);
                output.Write ("> " + result);
            }

            if (expectedResult.EndsWith (Environment.NewLine)) {
                output.WriteLine ();
            }
            Assert.AreEqual (expectedResult, output.ToString ());
        }

        public static AnyObject Evaluate (string source) {
            TopLevel topLevel = new TopLevel ();
            return topLevel.Evaluate (source);
        }
    }
}